package com.tms.admin.checkUtil;

import com.tms.admin.entity.TmsCustomer;
import com.tms.admin.entity.TmsDriver;
import com.tms.admin.entity.TmsLogistics;
import com.tms.admin.entity.enums.LogisticStatusEnum;
import com.tms.admin.entity.enums.LogisticsGetStatusEnum;
import com.tms.admin.handler.TMSException;
import com.tms.common.entity.ResultCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.regex.Pattern;

/**
 * @description 数据完整性检验：NULL、空值、号码格式
 */
public class DataCheck {

    /**
     * 对司机新增、编辑的数据进行空值检验
     * @param driver
     * @return
     */
    public static boolean isNullToDriver(TmsDriver driver){
        String name = driver.getName();
        String gender = driver.getGender();
        String phone = driver.getPhone();
        String truck_num = driver.getTruckNum();
        String state = driver.getState();
        String location = driver.getLocation();
        if (StringUtils.isAnyEmpty(name,gender,phone,truck_num,state,location)){
            return true;
        }
        return false;
    }

    /**
     * 对顾客新增、编辑的数据进行空值检验
     * @param customer
     * @return
     */
    public static boolean isNullToCustomer(TmsCustomer customer){
        String name = customer.getName();
        String phone = customer.getPhone();
        String address = customer.getAddr();
        String gender = customer.getGender();

        if (StringUtils.isAnyEmpty(name,phone,address,gender)){
            return true;
        }
        return false;
    }

    /**
     * 对物流新增的数据进行空值检验
     * @param logistic
     * @return
     */
    public static  boolean isNullToLogistic(TmsLogistics logistic){
        String code = logistic.getCode();
        String name = logistic.getName();
        String type = logistic.getType();
        String status = logistic.getStatus();
        String get_status = logistic.getGetStatus();
        String company = logistic.getCompany();

        Integer c_id_from = logistic.getCustomerIdFrom();
        Integer c_id_to = logistic.getCustomerIdTo();
        Integer d_id = logistic.getDriverId();

        if (StringUtils.isAnyEmpty(code,name,type,status,get_status,company))
            return true;

        if (c_id_from == null || c_id_to == null || d_id == null)
            return true;

        return false;
    }

    /**
     * 对物流编辑的数据进行空值检验
     * @param logistic
     * @return
     */
    public static String checkLogistic(TmsLogistics logistic){
        // check1 检验必填的字段是否为空
        if (isNullToLogistic(logistic))
            return "请检查必填项";
        // check2 “待发货”的快件没有起运时间、预计送达时间、取件时间 不能是已取件
        if (LogisticStatusEnum.WAITING.name().equals(logistic.getStatus())){
            if (logistic.getStartTime() != null)
                return "物流待发货，无法设置起送时间";
            if (logistic.getArrivedTime() != null)
                return "物流待发货，无法设置预计到达时间";
            if (logistic.getGetTime() != null)
                return "物流待发货，无法设置取件时间";
            if (LogisticsGetStatusEnum.YES.getName().equals(logistic.getGetStatus())) {
                return "物流待发货，无法取件";
            }
        }
        // check3 ”运输中“的快件必须有起运时间，预计送达时间，不能是已签收
        if (LogisticStatusEnum.TRANSPORTING.getName().equals(logistic.getStatus())){
            if (logistic.getStartTime() == null)
                return "物流已在运输，请设置起送时间";
            if (logistic.getArrivedTime() == null)
                return "物流还在运输中，请设置预计到达时间";
            if (LogisticsGetStatusEnum.YES.getName().equals(logistic.getGetStatus()))
                return "物流还在运输中，无法取件";
        }
        // check4 “已送达”的快件必须有起运时间、送达时间
        if (LogisticStatusEnum.ARRIVED.getName().equals(logistic.getStatus())){
            if (logistic.getStartTime() == null ||
                logistic.getArrivedTime() == null){
                return "物流已送达，请设置起运、送达时间";
            }
        }

        // ”待取件“的快件没有取件时间
//        if (LogisticsGetStatusEnum.NO.name().equals(logistic.getGetStatus())){
//            if (logistic.getGetTime() != null)
//                return "物流待取件，无法设置取件时间";
//        }
        // check5 ”已签收“的快件必须有取件时间,且物流状态必须是已送达
        if (LogisticsGetStatusEnum.YES.getName().equals(logistic.getGetStatus())){
            if (logistic.getGetTime() == null)
                return "物流已签收，请设置取件时间";
            if (!LogisticStatusEnum.ARRIVED.getName().equals(logistic.getStatus()))
                return "物流已签收，请检查物流运输状态";
        }
        // 检验物流时间先后的合法性
        Date startTime = logistic.getStartTime();
        Date arrivedTime = logistic.getArrivedTime();
        Date getTime = logistic.getGetTime();
        if (startTime != null && arrivedTime != null){
            // 判断物流送达时间是否大于起送时间
            if (startTime.after(arrivedTime))
                return "物流送达时间不能早于发货时间";
            if (getTime != null){
                if (getTime.before(startTime) || getTime.before(arrivedTime))
                    return "取件时间不能早于发货时间或到达时间";
            }
        }
        return "success";
    }

    /**
     * 对手机号码的格式进行检验
     * @param phoneNumber
     * @return
     */
    public static boolean isErrorToPhone(String phoneNumber){
        String regex = "^1[3-9]\\d{9}$";
        if (!Pattern.matches(regex,phoneNumber)){
            return true;
        }
        return false;
    }


}
